#include "octree.h"

octree::octree()
{
    valid=0;
    child = new octree*[8];
}

bool octree::check_bit(char num)
{
    unsigned char valid_bits=1;
    valid_bits = valid_bits << (num);
    if((valid_bits & valid) == valid_bits){
        ///bit already is true
        return true;
    }
    return false;

}

void octree::set_bit(char num)
{

    unsigned char valid_bits = 1;
    valid_bits = valid_bits << (num);
    valid = valid_bits | valid;

}

void octree::unset_bit(char num)
{

    unsigned char valid_bits = 2^8 - 2; ///11111110
    valid_bits = valid_bits << num;
    valid_bits = valid_bits | (2^num - 1);

    valid = valid_bits & valid;

    //unsigned char shift = valid >> num;

}

void octree::alloc_children()
{

    //child=new octree*[8];
    for(int i=0; i<8; i++)
    {
        child[i]=new octree;
        set_bit(i);
    }



}

void octree::alloc_child(char num)
{
    if(!check_bit(num))
    {
        child[num]=new octree;
        set_bit(num);
    }

}

void octree::dealloc_child(char num)
{
    if(check_bit(num))
    {
        unset_bit(num);
        delete child[num];
    }

}

void octree::set_child(char num, unsigned int rgb, unsigned int normal)
{

    if(!check_bit(num))
    {
        alloc_child(num);
        set_bit(num);
    }

    child[num]->vox.rgb=rgb;
    child[num]->vox.normal=normal;


}

octree * octree::next_octree(char num)
{

    return child[num];

}

octree::~octree()
{
//i 0 8 dealloc
    for(int i=0; i<8; i++){
        dealloc_child(i);
    }
    delete [] child;
}


